var $j = jQuery.noConflict();
/*----------global variables for account information---------------*/
var inputUsername;
var inputName;
var inputOldPass;
var inputNewPass;
var inputConfirmPass;
var inputEmail;
var inputBirthDate;

var spanUsername;
var spanName;
var spanOldPass;
var spanNewPass;
var spanConfirmPass;
var spanEmail;
var spanBirthDate;

var msgErrorUsername;
var msgErrorName;
var msgErrorConfirmPass;
var msgErrorPass;
var msgErrorEmail;
var msgErrorDate;

/*----------global variables for addresses---------------*/
var inputFullName;
var inputAddress_line_1;
var inputAddress_line_2;
var inputCountry;
var inputState;
var inputCity;
var inputZip_code;
var inputPhone_number;

var spanFullName;
var spanAddress_line_1;
var spanAddress_line_2;
var spanCountry;
var spanState;
var spanCity;
var spanZipCode;
var spanPhone;

var msgErrorFullName;
var msgErrorAddress_line_1;
var msgErrorAddress_line_2;
var msgErrorCountry;
var msgErrorState;
var msgErrorCity;
var msgErrorZipCode;
var msgErrorPhone;

var inputAddr_id;

function validateId(id, spanId, msgErrorId) {
	
	alert(id.attr("value"));
	if(id.attr("value") == '' || id.attr("value") < 1){
		id.addClass("error");
		spanId.addClass("error");
		msgErrorId.addClass("error");
		msgErrorId.attr("style", "visibility:visible");
		return false;
	}
	else{
		id.removeClass("error");
		spanId.removeClass("error");
		msgErrorId.removeClass("error");
		msgErrorId.attr("style", "visibility: hidden");
		return true;
	}
}

function validate(inputField, spanField, msgErrorField, maxLength) {
	if(inputField.val().length < 1 || inputField.val().length > maxLength){
		inputField.addClass("error");
		spanField.addClass("error");
		msgErrorField.addClass("error");
		msgErrorField.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputField.removeClass("error");
		spanField.removeClass("error");
		msgErrorField.removeClass("error");
		msgErrorField.attr("style", "visibility: hidden");
		return true;
	}
}

function validateUsername(){
	var pattern = new RegExp("^[a-zA-Z0-9]+$");

	
	//NO cumple longitud minima
	if(inputUsername.val().length < 1 || inputUsername.val().length > 15){
		inputUsername.addClass("error");
		spanUsername.addClass("error");
		msgErrorUsername.addClass("msgError");
		msgErrorUsername.attr("style", "visibility:visible");
		return false;
	}
	else if(!pattern.test(inputUsername.val())){
		inputUsername.addClass("error");
		spanUsername.addClass("error");
		msgErrorUsername.addClass("msgError");
		msgErrorUsername.attr("style", "visibility:visible");
		return false;
	}
	else{
		
		inputUsername.removeClass("error");
		spanUsername.removeClass("error");
		msgErrorUsername.removeClass("msgError");
		msgErrorUsername.attr("style", "visibility:hidden");
		return true;
	}
}

function validateName(){
	var pattern = new RegExp("^[a-zA-Z]+[a-zA-Z\ ]*$");
	if(inputName.val().length < 1 || inputName.val().length > 80){
		inputName.addClass("error");
		spanName.addClass("error");
		msgErrorName.addClass("msgError");
		msgErrorName.attr("style", "visibility:visible");
		return false;
	}
	else if(!pattern.test(inputName.val())){
		inputName.addClass("error");
		spanName.addClass("error");
		msgErrorName.addClass("msgError");
		msgErrorName.attr("style", "visibility:visible");
		return false;
	}
	else{
		
		inputName.removeClass("error");
		spanName.removeClass("error");
		msgErrorName.removeClass("msgError");
		msgErrorName.attr("style", "visibility:hidden");
		return true;
	}
}

function validateOldPass(){
	if(inputOldPass.val().length < 8 || inputOldPass.val().length > 15){
		inputOldPass.addClass("error");
		spanOldPass.addClass("error");
		msgErrorPass.addClass("msgError");
		msgErrorPass.attr("style", "visibility:visible");
		return false;
	}
	else{
		
		inputOldPass.removeClass("error");
		spanOldPass.removeClass("error");
		msgErrorPass.removeClass("msgError");
		msgErrorPass.attr("style", "visibility:hidden");
		return true;
	}
}

function validateNewPass(){
	if(inputNewPass.val().length > 15){
		inputNewPass.addClass("error");
		spanNewPass.addClass("error");
		return false;
	}
	else{
		
		inputNewPass.removeClass("error");
		spanNewPass.removeClass("error");
		return true;
	}
}

function validateConfirmPass(){
	if(inputConfirmPass.val().length > 15){
		inputConfirmPass.addClass("error");
		spanConfirmPass.addClass("error");
		msgErrorConfirmPass.addClass("msgError");
		msgErrorConfirmPass.attr("style", "visibility:visible");
		return false;
	}
	else if(inputConfirmPass.val() != inputNewPass.val()){
		inputConfirmPass.addClass("error");
		spanConfirmPass.addClass("error");
		msgErrorConfirmPass.addClass("msgError");
		msgErrorConfirmPass.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputConfirmPass.removeClass("error");
		spanConfirmPass.removeClass("error");
		msgErrorConfirmPass.removeClass("msgError");
		msgErrorConfirmPass.attr("style", "visibility:hidden");
		return true;
	}
}

function validateEmail(){
	var pattern = new RegExp("^[a-zA-Z0-9.-_]+@[a-zA-Z0-9.-_]+[a-zA-Z]{2,4}$");

	if(inputEmail.val().length < 1 || inputEmail.val().length > 128){
		inputEmail.addClass("error");
		spanEmail.addClass("error");
		msgErrorEmail.addClass("msgError");
		msgErrorEmail.attr("style", "visibility:visible");
		return false;
	}
	else if(!pattern.test(inputEmail.val())){
		inputEmail.addClass("error");
		spanEmail.addClass("error");
		msgErrorEmail.addClass("msgError");
		msgErrorUsername.attr("style", "visibility:visible");
		return false;
	}
	else{
		
		inputEmail.removeClass("error");
		spanEmail.removeClass("error");
		msgErrorEmail.removeClass("msgError");
		msgErrorEmail.attr("style", "visibility:hidden");
		return true;
	}
}

function validateBirthDate(){
	var pattern = new RegExp("^[1-2][0-9][0-9][0-9]\-(([0-9])|(0[1-9])|(1[0-2]))\-(([0-9])|(0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))$");

	if(!pattern.test(inputBirthDate.val())){
		inputBirthDate.addClass("error");
		spanBirthDate.addClass("error");
		return false;
	}
	else{
		inputBirthDate.removeClass("error");
		spanBirthDate.removeClass("error");
		return true;
	}
}

function validateFN() {
	if(inputFullName.val().length < 1 || inputFullName.val() > 80){
		inputFullName.addClass("error");
		spanFullName.addClass("error");
		msgErrorFullName.addClass("error");
		msgErrorFullName.attr("style", "visibility:visible");
		return false;
			
	}
	else{
		inputFullName.removeClass("error");
		spanFullName.removeClass("error");
		msgErrorFullName.removeClass("error");
		msgErrorFullName.attr("style", "visibility: hidden");
		return true
	}		
}

function validateLine1() {
	if(inputAddress_line_1.val().length < 1 || inputAddress_line_1.val() > 80){
		inputAddress_line_1.addClass("error");
		spanAddress_line_1.addClass("error");
		msgErrorAddress_line_1.addClass("error");
		msgErrorAddress_line_1.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputAddress_line_1.removeClass("error");
		spanAddress_line_1.removeClass("error");
		msgErrorAddress_line_1.removeClass("error");
		msgErrorAddress_line_1.attr("style", "visibility: hidden");
		return true;
	}		
}

function validateCountry() {
	if(inputCountry.attr("value") < 1){
		inputCountry.addClass("error");
		spanCountry.addClass("error");
		msgErrorCountry.addClass("error");
		msgErrorCountry.attr("style", "visibility:visible");
		return false;	
	}
	else{
		inputCountry.removeClass("error");
		spanCountry.removeClass("error");
		msgErrorCountry.removeClass("error");
		msgErrorCountry.attr("style", "visibility: hidden");
		return true;
	}		
}

function validateState() {
	if(inputState.attr("value") < 1){
		inputState.addClass("error");
		spanState.addClass("error");
		msgErrorState.addClass("error");
		msgErrorState.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputState.removeClass("error");
		spanState.removeClass("error");
		msgErrorState.removeClass("error");
		msgErrorState.attr("style", "visibility: hidden");
		return true;
	}		
}

function validateCity() {
	if(inputCity.val().length < 1 || inputCity.val().length > 80){
		inputCity.addClass("error");
		spanCity.addClass("error");
		msgErrorCity.addClass("error");
		msgErrorCity.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputCity.removeClass("error");
		spanCity.removeClass("error");
		msgErrorCity.removeClass("error");
		msgErrorCity.attr("style", "visibility: hidden");
		return true;
	}		
}

function validateZipCode() {
	if(inputZipCode.val().length < 1 || inputZipCode.val().length > 8){
		inputZipCode.addClass("error");
		spanZipCode.addClass("error");
		msgErrorZipCode.addClass("error");
		msgErrorZipCode.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputZipCode.removeClass("error");
		spanZipCode.removeClass("error");
		msgErrorZipCode.removeClass("error");
		msgErrorZipCode.attr("style", "visibility: hidden");
		return true;
	}		
}

function validatePhone_number() {
	if(inputPhone_number.val().length < 1 || inputPhone_number.val().length > 80){
		inputPhone_number.addClass("error");
		spanPhone.addClass("error");
		msgErrorPhone.addClass("error");
		msgErrorPhone.attr("style", "visibility:visible");
		return false;
	}
	else{
		inputPhone_number.removeClass("error");
		spanPhone.removeClass("error");
		msgErrorPhone.removeClass("error");
		msgErrorPhone.attr("style", "visibility: hidden");
		return true;
	}		
}

